package com.changgou.goods.controller;

import com.changgou.goods.pojo.Para;
import com.changgou.goods.service.ParaService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wzw
 * @Date: 2020/12/9 16:51
 * @version: 1.8
 */
@RestController
@RequestMapping("/para")
@CrossOrigin
public class ParaController {
    //注入service
    @Autowired
    private ParaService paraService;

    /**
     * @author wzw
     * 添加功能
     * @Date 16:59 2020/12/9
     * @param para
     * @return entity.Result
     **/
    @PostMapping
    public Result add(@RequestBody(required = false)Para para){
        //实现功能
        paraService.add(para);
        //返回结果
        return new Result(true, StatusCode.OK,"参数添加成功");
    }

    /**
     * @author wzw
     * 删除功能
     * @Date 17:24 2020/12/9
     * @param id
     * @return entity.Result
    **/
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id")Integer id){
        //实现功能
        paraService.delete(id);
        //返回值
        return new Result(true,StatusCode.OK,"参数删除成功");
    }

    /**
     * @author wzw
     * 修改
     * @Date 17:33 2020/12/9
     * @param para 对象
     * @param id
     * @return entity.Result
    **/
    @PutMapping("/{id}")
    public Result update(@RequestBody(required = false)Para para,@PathVariable Integer id){
        //设置主键值
        para.setId(id);
        //修改数据
        paraService.update(para);
        //返回
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * @author wzw
     * 查询一个
     * @Date 17:44 2020/12/9
     * @param id
     * @return entity.Result
    **/
    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id")Integer id){
        //实现功能:查询条件
        Para byId = paraService.findById(id);
        //返回
        return new Result(true, StatusCode.OK, "参数查询成功", byId);
    }

    /**
     * @author wzw
     * 查询所有
     * @Date 18:38 2020/12/9
     * @param
     * @return entity.Result
    **/
    @GetMapping
    public Result findAll(){
        //实现功能
        List<Para> all = paraService.findAll();
        //返回值
        return new Result(true, StatusCode.OK, "查询所有成功", all);
    }

    /**
     * @author wzw
     * 分页查询
     * @Date 18:08 2020/12/9
     * @param page
     * @param size
     * @return entity.Result
    **/
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable(value = "page")Integer page,@PathVariable(value = "size")Integer size){
        //实现功能
        PageInfo<Para> page1 = paraService.findPage(page, size);
        //返回值
        return new Result(true, StatusCode.OK, "分页查询成功", page1);
    }

    /**
     * @author wzw
     * 多条件查询
     * @Date 19:29 2020/12/9
     * @param para
     * @return
     **/
    @PostMapping("/search")
    public Result findList(@RequestBody(required = false)Para para){
        //实现功能多条件
        List<Para> list = paraService.findList(para);
        //返回
        return new Result(true, StatusCode.OK, "多条件查询成功", list);
    }

    /**
     * @author wzw
     * 分页+条件查询
     * @Date 18:55 2020/12/9
     * @param para
     * @param page
     * @param size
     * @return entity.Result
    **/
    @PostMapping("/search/{page}/{size}")
    public Result findPage(@RequestBody(required = false)Para para,@PathVariable(value = "page")Integer page,@PathVariable(value = "size")Integer size){
        PageInfo<Para> page1 = paraService.findPage(para, page, size);
        return new Result(true, StatusCode.OK, "分页+条件成功", page1);
    }

    /**
     * @author wzw
     * 根据分类ID查询参数列表
     * @Date 8:29 2020/12/11
     * @param categoryId
     * @return entity.Result
    **/
    @GetMapping("/category/{id}")
    public Result findByCategoryId(@PathVariable(value = "id")Integer categoryId){
        //实现功能
        List<Para> paraList = paraService.findByCategoryId(categoryId);

        //返回结果集
        return new Result(true, StatusCode.OK, "根据分类ID查询参数列表成功",paraList);
    }

}
